<?php
/*
* Created by DevilKing
* Date: 2019-06-08
*Time: 16:19
*/

namespace app\lib\jingdong;

use think\facade\Cache;

class Jing
{
    /** key*/
    private $appKey = "653A5E8209148E4CB8DCF955091C655C";
    /** secret*/
    private $appSecret = "db2d855b195f42f39c630327f871540e";
    private $grantType = "authorization_code";
   // private $code = "Qo4jga";

    /**
     * 生成签名
     */
    public function createSign($attributes)
    {
        //按照规则拼接字符串
        $str = "360buy_param_json" . $attributes['360buy_param_json'] . "access_token" . $attributes['access_token'] . "app_key" . $this->appKey . "method" . $attributes['method']. "timestamp" . $attributes['timestamp'] . "v" . $attributes['v'];
        $md5_str = $this->appSecret . $str . $this->appSecret;
        //MD5加密
        $sign = md5($md5_str);
        //转大写
        $sign = strtoupper($sign);
        return $sign;
    }

    /*
     * 校验access_token
     * */
    protected function access_token()
    {

        $access_token   =   Cache::get('access_token');
        $refresh_token   =   Cache::get('refresh_token');

        if($refresh_token){
            $this->refresh_token();
        }else{
            $data['app_key']        =   $this->appKey;
            $data['app_secret']     =   $this->appSecret;
            $data['grant_type']     =   $this->grantType;
            $data['code']           =   "7sCvz7";

            $url    =   "https://open-oauth.jd.com/oauth2/access_token?";
            $res = $this->https_request($url,$data);

            $jsoninfo = json_decode($res, true);

            $access_token   =   $jsoninfo['access_token'] ?? '';
            $refresh_token   =   $jsoninfo['refresh_token'] ?? '';
            Cache::set('access_token', $access_token, 7200);//minutes
            Cache::set('refresh_token', $refresh_token);
        }
        return $access_token;
    }

    /**
     * 刷新accree_token
     * */
    public function refresh_token()
    {
        $refresh_token   =   Cache::get('refresh_token');
        $url    =   "https://open-oauth.jd.com/oauth2/refresh_token?";

        $data['app_key']        =   $this->appKey;
        $data['app_secret']     =   $this->appSecret;
        $data['grant_type']     =   "refresh_token";
        $data['refresh_token']  =   $refresh_token;

        $res = $this->https_request($url,$data);

        $jsoninfo = json_decode($res, true);

        $access_token   =   $jsoninfo['access_token'] ?? '';

        $refresh_token   =   $jsoninfo['refresh_token'] ?? '';

        Cache::set('access_token', $access_token, 7200);//minutes
        Cache::set('refresh_token', $refresh_token);


    }

    /**
     * 发送请求
     * @param $url：地址
     * @param null $data：post的数据
     * @return mixed：请求返回的结果
     */
    public static function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}